<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Escort_description_entity{
	var $current_dbprefix = "";
	var $CI;
	var $id= 0;
	var $escort_id = 0;
	var $language_id = 0;
	var $description = 0;
	var $timestamp = 0;
	var $trans_mode = 1;
	var $is_source = false;
	
	function __construct() {
		$this->CI = &get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setEscortId($input){
		$this->escort_id=$input;
	}
	function setLanguageId($input){
		$this->language_id =$input;
	}
	function setDescription($input){
		$this->description =$input;
	}
	function setTransMode($input){
		$this->trans_mode=$input;
	}
	function setTimeStamp($input){
		$this->timestamp=$input;
	}
	function setIsSource($input){
		$this->is_source=$input;
	}
	
	function getId(){
		return $this->id;
	}
	function getEscortId(){
		return $this->escort_id;
	}
	function getLanguageId(){
		return $this->language_id;
	}
	function getDescription(){
		return $this->description;
	}
	function getTransMode(){
		return $this->trans_mode;
	}
	function getTimeStamp(){
		return $this->timestamp;
	}
	function getIsSource(){
		return $this->is_source;
	}
	
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		
		if($cmd == 'id'){
			$result = $this->CI->db->where('id',$input)->get(TBL_ESCORT_DESCRIPTION)->result();
		}else{
			return false;
		}
		if( !empty($result) ){
			$this->id = $result[0]->id;
			$this->escort_id = $result[0]->escort_id;
			$this->language_id = $result[0]->language_id;
			$this->description = $result[0]->description;
			$this->timestamp = $result[0]->timestamp;
			$this->trans_mode = $result[0]->trans_mode;
			$this->is_source = $result[0]->is_source;
			
			return $this;
		}
		return false;
	}
	function getEscortDescriptionList(){
		$this->CI->db->set_dbprefix('default_');
		return $this->CI->db->get(TBL_ESCORT_DESCRIPTION)->result();
	}
	function save(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('escort_id',$this->getEscortId());
		$this->CI->db->set('language_id',$this->getLanguageId());
		$this->CI->db->set('description',$this->description);
		$this->CI->db->set('timestamp', $this->timestamp);
		$this->CI->db->set('trans_mode', $this->trans_mode);
		$this->CI->db->set('is_source', $this->is_source);
		
		$this->CI->db->insert(TBL_ESCORT_DESCRIPTION);
		return $this->CI->db->insert_id();
	}
	function update(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('escort_id',$this->getEscortId());
		$this->CI->db->set('language_id',$this->getLanguageId());
		$this->CI->db->set('description',$this->description);
		$this->CI->db->set('timestamp', $this->timestamp);
		$this->CI->db->set('trans_mode', $this->trans_mode);
		$this->CI->db->set('is_source', $this->is_source);
		
		return $this->CI->db->where('id', $this->id)->update(TBL_ESCORT_DESCRIPTION);
	}
	function delete($id){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->where('id',$id);
		$this->CI->db->delete(TBL_ESCORT_DESCRIPTION);
	}
	
	//translate
	function autoTranslate($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where( array('escort_id'=>$escort_id) )
				->get(TBL_ESCORT_DESCRIPTION)->result();
				
		$source = $this->CI->db->where( array('escort_id'=>$escort_id , 'is_source'=>1) )
				->get(TBL_ESCORT_DESCRIPTION)->result();
		
		if($source){
			$source_desc_id = $source[0]->id;
		}else{
			$source_desc_id = $rs[0]->id; 
		}
		$F = $this->init('id',$source_desc_id);
		$FL = clone_( $this->CI->language_entity->init('id', $F->language_id) );
		
		$from = $FL->code;
		$s = $F->getDescription();
			
		foreach($rs as $item){
			if( $item->language_id != $F->language_id ){
				//$TL = $this->CI->language_entity->getEscortLanguagesInfo($item->language_id);
				$TL = clone_ ( $this->CI->language_entity->init('id', $item->language_id) );
				$to = $TL->code;
				if($item->trans_mode == 1){
					$st = $this->googleTranslate($from, $to, $s);
					$this->CI->db->set('description',$st);
					$this->CI->db->where('id', $item->id)->update(TBL_ESCORT_DESCRIPTION);
				}
			}
		}
	}
	
	function googleTranslate($from, $to, $string){
		$strarray = explode(PHP_EOL, $string);
		$strarrayreturn = array();
		foreach($strarray as $s){
			$strarrayreturn[] = google_translate($from, $to, $s);
		}
		return implode(PHP_EOL,$strarrayreturn);
	}
	
	//endclass
}